package com.card.manager.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.framework.page.model.PageModel;
import com.card.manager.domain.Abb;
import com.card.manager.services.AbbService;

@Controller
@RequestMapping("/abb")
public class AbbController {
	@Autowired(required=true) 
	private AbbService abbServices;
	@RequestMapping("/index")
	public String aaa(){
		return "abbList";
	}
	@RequestMapping(value="/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryList(HttpServletRequest request,PageModel pageModel){
		Map<String,String> params = new HashMap<String,String>();
		@SuppressWarnings("unchecked")
		Enumeration<String> en = request.getParameterNames();
		while(en.hasMoreElements()){
			String k = en.nextElement();
			if(k.startsWith("q@")){
				params.put(k.substring(2), request.getParameter(k));
			}
		}
		return abbServices.selectAbbForPage(params, pageModel);
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/exportQueryList",method=RequestMethod.GET)
	public void exportQueryList(HttpServletRequest request,HttpServletResponse response,PageModel pageModel) throws IOException{
		Map<String,String> params = new HashMap<String,String>();
		//组合过滤条件.
		Enumeration<String> en = request.getParameterNames();
		while(en.hasMoreElements()){
			String k = en.nextElement();
			if(k.startsWith("q@")){
				params.put(k.substring(2), request.getParameter(k));
			}
		}
		pageModel.setRows(0);
		//查询结果集.
		Map<String, Object> map = abbServices.selectAbbForPage(params, pageModel);
		String[] titles = {"充值日期 ","卡号","充值流水号","充值类型","电字帐户充值前金额","充值金额","余额","电子钱包充值前金额",
				"充值金额","余额","操作员"};
		int[] columnWidth = {3000,2500,2500,2500,5000,2000,2000,5000,
				2000,2000,2000};
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("充值明细列表");
		//设置横向排版
		sheet.getPrintSetup().setLandscape(true);
		//设置A4纸
		sheet.getPrintSetup().setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
//		sheet.setDefaultColumnWidth(15);
		sheet.autoSizeColumn(0);
		HSSFCellStyle headerStyle = wb.createCellStyle(); 
		headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 
		headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
		Font font = wb.createFont();
		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		headerStyle.setFont(font);
		int rowIndex = 0;
		//添加标题
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titles.length - 1));
		HSSFRow caption = sheet.createRow(rowIndex);
		caption.createCell(0,HSSFCell.CELL_TYPE_STRING).setCellValue("充值明细列表");
		HSSFCellStyle captionStyle = wb.createCellStyle(); 
		captionStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中    
		HSSFFont captionFont = wb.createFont();    
		captionFont.setFontName("黑体");    
		captionFont.setFontHeightInPoints((short) 16);
		captionStyle.setFont(captionFont);
		caption.getCell(0).setCellStyle(captionStyle);
		caption.setHeight((short)500);
		rowIndex++;
		//添加表头
		HSSFRow title = sheet.createRow(rowIndex);
		rowIndex++;
		for(int i = 0;i < titles.length;i++){
			title.createCell(i,HSSFCell.CELL_TYPE_STRING).setCellValue(titles[i]);
			title.getCell(i).setCellStyle(headerStyle);
			sheet.setColumnWidth(i, columnWidth[i]);
		}
		HSSFCellStyle bodyStyle = wb.createCellStyle(); 
		bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		bodyStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		//添加数据
		HSSFRow rowData = null;
		
		List<Abb> itemList = (List<Abb>)map.get("rows");
		List<Abb> footer =  (List<Abb>)map.get("footer");
		itemList.addAll(footer);
		for(Abb abb : itemList){
			if(abb == null)
				continue;
			rowData = sheet.createRow(rowIndex);
			//添加充值日期
			rowData.createCell(0, HSSFCell.CELL_TYPE_STRING)
				.setCellValue(abb.getCreatdatetime() == null ? "" : DateFormatUtils.format(abb.getCreatdatetime(),"yyyy-MM-dd"));
			//添加卡号
			rowData.createCell(1, HSSFCell.CELL_TYPE_STRING)
				.setCellValue(abb.getAbaid() == null ? "" : abb.getAbaid());
			//添加充值流水号
			String id = abb.getId() == null ? null : String.valueOf(abb.getId());
			if(id == null){
				id = "";
			}else{
				while(id.length() < 8){
					id = "0" + id;
				}
			}
			rowData.createCell(2, HSSFCell.CELL_TYPE_STRING).setCellValue(id);
			//添加充值类型
			rowData.createCell(3, HSSFCell.CELL_TYPE_STRING)
				.setCellValue(abb.getCreditflag() == null ? "" : abb.getCreditflag() == 1 ? "充值" : "冲账");
			//添加电字帐户充值前金额
			double emb = abb.getEmoneybalance() == null ? 0 : abb.getEmoneybalance();
			rowData.createCell(4, HSSFCell.CELL_TYPE_STRING).setCellValue(emb);
			//添加充值金额
			double em = abb.getEmoney() == null ? 0 : abb.getEmoney();
			rowData.createCell(5, HSSFCell.CELL_TYPE_STRING).setCellValue(em);
			//添加余额
			String ye = abb.getCreditflag() == null ? "" : abb.getCreditflag() == 1 ? String.valueOf(emb + em) : String.valueOf(emb - em);
			rowData.createCell(6, HSSFCell.CELL_TYPE_STRING).setCellValue(ye);
			//添加电字钱包充值前金额
			double mb = abb.getMoneybalance() == null ? 0 : abb.getMoneybalance();
			rowData.createCell(7, HSSFCell.CELL_TYPE_STRING).setCellValue(mb);
			//添加充值金额
			double m = abb.getMoney() == null ? 0 : abb.getMoney();
			rowData.createCell(8, HSSFCell.CELL_TYPE_STRING).setCellValue(m);
			//添加余额
			String e = abb.getCreditflag() == null ? "" : abb.getCreditflag() == 1 ? String.valueOf(mb + m) : String.valueOf(mb - m);
			rowData.createCell(9, HSSFCell.CELL_TYPE_STRING).setCellValue(e);
			//添加操作人
			rowData.createCell(10, HSSFCell.CELL_TYPE_STRING).setCellValue(StringUtils.isBlank(abb.getCreatuserid()) ? "" : abb.getCreatuserid());
			rowIndex++;
		}
		wb.setPrintArea(0, 0, titles.length-1, 0, rowIndex);
		
		response.setContentType("application/vnd.ms-excel");
		Date date = new Date();
		
		String headerDisp = "attachment;filename=充值明细表_" + DateFormatUtils.format(date,"yyyyMMddHHmmss") + ".xls";
		headerDisp = new String(headerDisp.getBytes("gbk"), "iso8859-1");
		response.setHeader("content-disposition", headerDisp);
		wb.write(response.getOutputStream());
	}
}
